/* ----------------------------------------------------------------------
 * File: 		token.cpp
 * Created by:		Taylor Wood
 * Email:		Tdwood2@asu.edu
 * Student ID:		1202553801
 *
 * Description:
 * This file contains all of the linked list methods for a token.
 * ----------------------------------------------------------------------*/
#include "token.h"

// Pointer to the next token
token * curr = new token;
// Pointer to the head token
token * head = new token;

/*
 * Function:		createList
 * Argument(s):		* token
 * Return:		int
 * 
 * Description: Creates the linked list. Returns 0 if the list was
 * created successfully. Returns -1 otherwise.
 */
int createList(token * list) {

	cout << "Creating the list.\n";

	list->next = NULL;
	head = curr = list;	

	return 0;
}


/*
 * Function:		addToList
 * Argument(s):		* token
 * Return:		int
 * 
 * Description: Adds a token onto the list. Returns a 0 when
 * added successfully, otherwise -1.
 */
int addToList(token * tok) {


	// the default value for line is 0. Thus, 
	if (head->line ==0) 
		return createList(tok);


	//cout << "Adding to the list\n";

	// Everything is good, add the token to the list
	tok->next = NULL;
	curr->next = tok;
	curr = tok;

	return 0;
}

/*
 * Function:		getCurrentToken
 * Argument(s):		void
 * Return:		token *
 * 
 * Description: Returns the current token.
 */
token * getCurrToken() {
	return curr;
}

/*
 * Function:		incrToken
 * Argument(s):		void
 * Return:		int
 * 
 * Description: Increments the token. Returns 0 if successful, otherwise -1
 */
int incrToken() {
	if (curr != NULL) {
		curr = curr->next;
		return 0;
	}
	return -1;
}

/*
 * Function:		setHead
 * Argument(s):		void
 * Return:		void
 * 
 * Description: Points curr to head
 */
void setHead() {
	curr = head;
}

/*
 * Function:		printList
 * Argument(s):		void
 * Return:		void
 * 
 * Description: Starts at the head of the list and prints
 * every token until the end. This is for testing 
 * purposes.
 */
void printList() {

	token * tok;
	tok = new token;
	tok = head;

	cout << "\n----Printing the List----\n";
	while (tok != NULL) {
		cout << "{\"" << tok->name << "\",\"" << tok->word << "\",\"" << tok->line << "\"}\n";
		//std::cout << "Line #" << tok->line << std::endl;
		tok = tok->next;
	}

	delete tok;
	delete head;

}
